/**
 * 
 */
package org.yelongframework.model.support.spring.beans.propertyeditors;

import java.text.NumberFormat;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.yelongframework.model.property.nulls.ModelNullProperty;

/**
 * 自定义模型数值装订器
 * 
 * @since 1.0.0
 */
public class ModelCustomNumberEditor extends CustomNumberEditor {

	protected final Class<? extends Number> numberClass;

	public ModelCustomNumberEditor(Class<? extends Number> numberClass, boolean allowEmpty)
			throws IllegalArgumentException {
		super(numberClass, allowEmpty);
		this.numberClass = numberClass;
	}

	public ModelCustomNumberEditor(Class<? extends Number> numberClass, NumberFormat numberFormat, boolean allowEmpty)
			throws IllegalArgumentException {
		super(numberClass, numberFormat, allowEmpty);
		this.numberClass = numberClass;
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if (StringUtils.isBlank(text)) {
			if (Byte.class == numberClass) {
				setValue(ModelNullProperty.BYTE_NULL);
			} else if (Short.class == numberClass) {
				setValue(ModelNullProperty.SHORT_NULL);
			} else if (Integer.class == numberClass) {
				setValue(ModelNullProperty.INTEGER_NULL);
			} else if (Long.class == numberClass) {
				setValue(ModelNullProperty.LONG_NULL);
			} else if (Float.class == numberClass) {
				setValue(ModelNullProperty.FLOAT_NULL);
			} else if (Double.class == numberClass) {
				setValue(ModelNullProperty.DOUBLE_NULL);
			} else {
				super.setAsText(text);
			}
		} else {
			super.setAsText(text);
		}
	}

}
